package com.i2works.smartluolong.manage.controller.court;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.web.entity.court.*;
import com.i2works.smartluolong.web.service.court.*;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 婚姻审批
 *
 * @author Li haoyu
 */
@Controller
@RequestMapping("/courtLawsuit")
public class CourtLawsuitController extends BaseModule {
    @Autowired
    ICourtCarService carService;
    @Autowired
    ICourtAssetService courtAssetService;
    @Autowired
    ICourtSuggestService suggestService;
    @Autowired
    ICourtLawsuitService lawsuitService;
    @Autowired
    ICourtHouseService houseService;
    @Autowired
    ICourtRelativesService relativesService;
    @Autowired
    ICourtOtherService otherService;

    /**
     * 认领页面
     */
    @RequestMapping("/claim")
    public String claim(Model model) {
        model.addAttribute("state", "index");
        return "/marriage/apply";
    }

    /**
     * 审核页面
     */
    @RequestMapping("/assessors")
    public String assessor(Model model) {
        model.addAttribute("state", "assessor");
        return "/marriage/apply";
    }

    /**
     * 审核通过页面
     */
    @RequestMapping("/finish")
    public String pass(Model model) {
        model.addAttribute("state", "pass");
        return "/marriage/apply";
    }

    /**
     * 退回修改页面
     */
    @RequestMapping("/back")
    public String send(Model model) {
        model.addAttribute("state", "send");
        return "/marriage/apply";
    }


    /**
     * 查看婚姻诉讼申请,未认领界面数据
     */
    @RequestMapping("/index")
    @ResponseBody
    public Result index(CourtLawsuitQuery lawsuitQuery) {
        List<CourtLawsuitDO> courtLawsuitDOList = new ArrayList<>();
        try {
            lawsuitQuery.setStage(0);
            lawsuitQuery.setState(1);
            int count = lawsuitService.queryCount(lawsuitQuery);
            if (count > 0) {
                courtLawsuitDOList = lawsuitService.queryList(lawsuitQuery);
            }
            return R.success(courtLawsuitDOList, count);
        } catch (Exception e) {
            logger.error("查看婚姻诉讼出错:" + e.getMessage());
            return R.error("查看婚姻诉讼出错");
        }
    }

    /**
     * 查看审核事件
     */
    @RequestMapping("/assessor")
    @ResponseBody
    public Result check(CourtLawsuitQuery lawsuitQuery) {
        List<CourtLawsuitDO> courtLawsuitDOList = new ArrayList<>();
        try {
            lawsuitQuery.setStage(1);
            lawsuitQuery.setState(1);
            int count = lawsuitService.queryCount(lawsuitQuery);
            if (count > 0) {
                courtLawsuitDOList = lawsuitService.queryList(lawsuitQuery);
            }
            return R.success(courtLawsuitDOList, count);
        } catch (Exception e) {
            logger.error("查看婚姻审核中的事件出错:" + e.getMessage());
            return R.error("查看婚姻审核事件出错");
        }
    }

    /**
     * 查看审核通过事件
     */
    @RequestMapping("/pass")
    @ResponseBody
    public Result pass(CourtLawsuitQuery lawsuitQuery) {
        List<CourtLawsuitDO> courtLawsuitDOList = new ArrayList<>();
        try {
            lawsuitQuery.setStage(2);
            lawsuitQuery.setState(1);
            int count = lawsuitService.queryCount(lawsuitQuery);
            if (count > 0) {
                courtLawsuitDOList = lawsuitService.queryList(lawsuitQuery);
            }
            return R.success(courtLawsuitDOList, count);
        } catch (Exception e) {
            logger.error("查看婚姻审核通过出错:" + e.getMessage());
            return R.error("查看婚姻审核通过出错");
        }

    }

    /**
     * 查看退回修改事项
     */
    @RequestMapping("/send")
    @ResponseBody
    public Result back(CourtLawsuitQuery lawsuitQuery) {
        List<CourtLawsuitDO> courtLawsuitDOList = new ArrayList<>();
        try {
            lawsuitQuery.setStage(3);
            lawsuitQuery.setState(1);
            int count = lawsuitService.queryCount(lawsuitQuery);
            if (count > 0) {
                courtLawsuitDOList = lawsuitService.queryList(lawsuitQuery);
            }
            return R.success(courtLawsuitDOList, count);
        } catch (Exception e) {
            logger.error("查看婚姻退回事件出错:" + e.getMessage());
            return R.error("查看婚姻退回事件出错");
        }
    }

    /**
     * 查看诉讼详情
     */
    @RequestMapping("/details")
    public String details(Integer lawsuitId,String site, Model model) {
        CourtLawsuitQuery courtLawsuitQuery = new CourtLawsuitQuery();
        try {
            courtLawsuitQuery.setId(lawsuitId);
            CourtLawsuitDO courtLawsuitDO = lawsuitService.query(courtLawsuitQuery);
            //获取财产信息
            if (courtLawsuitDO.getHaveAsset() == 0) {
                model.addAttribute("asset", null);
            } else {
                CourtAssetQuery courtAssetQuery = new CourtAssetQuery();
                courtAssetQuery.setLawsuitId(lawsuitId);
                List<CourtAssetDO> courtAssetDOList = courtAssetService.queryList(courtAssetQuery);
                model.addAttribute("asset", courtAssetDOList);
            }
            //获取子女信息
            if (courtLawsuitDO.getHaveSon() == 0) {
                model.addAttribute("son", null);
            } else {
                CourtRelativesQuery relativesQuery = new CourtRelativesQuery();
                relativesQuery.setLawsuitId(lawsuitId);
                List<CourtRelativesDO> courtRelativesDOS = relativesService.queryList(relativesQuery);
                model.addAttribute("son", courtRelativesDOS);
            }
            //获取车辆信息
            if (courtLawsuitDO.getHaveCar() == 0) {
                model.addAttribute("car", null);
            } else {
                CourtCarQuery carQuery = new CourtCarQuery();
                carQuery.setLawsuitId(lawsuitId);
                List<CourtCarDO> carDOS = carService.queryList(carQuery);
                model.addAttribute("car", carDOS);
            }
            //获取房产信息
            if (courtLawsuitDO.getHaveHouse() == 0) {
                model.addAttribute("house", null);
            } else {
                CourtHouseQuery houseQuery = new CourtHouseQuery();
                houseQuery.setLawsuitId(lawsuitId);
                List<CourtHouseDO> houseDOS = houseService.queryList(houseQuery);
                model.addAttribute("house", houseDOS);
            }
            //其他财产
            CourtOtherQuery courtOtherQuery = new CourtOtherQuery();
            courtOtherQuery.setLawsuitId(lawsuitId);
            int otherCount = otherService.queryCount(courtOtherQuery);
            if (otherCount > 0) {
                List<CourtOtherDO> otherDOS = otherService.queryList(courtOtherQuery);
                model.addAttribute("other", otherDOS);
            } else {
                model.addAttribute("other", null);
            }
            //审批日志
            CourtSuggestQuery suggestQuery = new CourtSuggestQuery();
            suggestQuery.setLawsuitId(lawsuitId);
            int suggestCount = suggestService.queryCount(suggestQuery);
            if (suggestCount > 0) {
                List<CourtSuggestDO> suggestDOS = suggestService.queryList(suggestQuery);
                model.addAttribute("suggest", suggestDOS);
            } else {
                model.addAttribute("suggest", null);
            }
            //申报信息
            CourtLawsuitQuery lawsuitQuery = new CourtLawsuitQuery();
            lawsuitQuery.setId(lawsuitId);
            int lawsuitCount = lawsuitService.queryCount(lawsuitQuery);
            if (lawsuitCount > 0) {
                List<CourtLawsuitDO> lawsuitDOS = lawsuitService.queryList(lawsuitQuery);
                model.addAttribute("lawsuit", lawsuitDOS);
            } else {
                model.addAttribute("lawsuit", null);
            }
        } catch (Exception e) {
            logger.error("查看诉讼详情出错:" + e.getMessage());
        }
        return "/marriage/"+site;
    }

    /**
     * 认领受理
     */
    @RequestMapping("/accept")
    @ResponseBody
    public Result accept(Integer lawsuitId) {
        CourtLawsuitDO courtLawsuitDO = new CourtLawsuitDO();
        CourtSuggestDO suggestQuery = new CourtSuggestDO();
        try {
            courtLawsuitDO.setId(lawsuitId);
            courtLawsuitDO.setStage(1);
            courtLawsuitDO.setUpdateName(getUserName());
            lawsuitService.update(courtLawsuitDO);
            suggestQuery.setCheckName(getUserName());
            suggestQuery.setLawsuitId(lawsuitId);
            suggestQuery.setStage(1);
            suggestQuery.setSuggest("认领审核");
            suggestService.save(suggestQuery);
            return R.success("认领成功");
        } catch (Exception e) {
            logger.error("认领失败:" + e.getMessage());
            return R.error("认领失败");
        }
    }

    /**
     * 删除诉讼事项
     */
    @RequestMapping("/deleteLawsuit")
    @ResponseBody
    public Result deleteLawsuit(Integer lawsuitId) {
        CourtLawsuitQuery courtLawsuitQuery = new CourtLawsuitQuery();
        try {
            courtLawsuitQuery.setId(lawsuitId);
            CourtLawsuitDO lawsuitDO = lawsuitService.query(courtLawsuitQuery);
            if (lawsuitDO.getHaveHouse() == 1) {
                courtAssetService.deleteForLawsuit(lawsuitId);
            }
            if (lawsuitDO.getHaveCar() == 1) {
                carService.deleteForLawsuit(lawsuitId);
            }
            if (lawsuitDO.getHaveAsset() == 1) {
                courtAssetService.deleteForLawsuit(lawsuitId);
            }
            if (lawsuitDO.getHaveSon() == 1) {
                relativesService.deleteForLawsuit(lawsuitId);
            }
            CourtOtherQuery otherQuery = new CourtOtherQuery();
            otherQuery.setLawsuitId(lawsuitId);
            List<CourtOtherDO> courtOtherDOS = otherService.queryList(otherQuery);
            if (CollectionUtil.isNotEmpty(courtOtherDOS)) {
                otherService.deleteForLawsuit(lawsuitId);
            }
            CourtSuggestQuery suggestQuery = new CourtSuggestQuery();
            suggestQuery.setLawsuitId(lawsuitId);
            List<CourtSuggestDO> suggestDOS = suggestService.queryList(suggestQuery);
            if (CollectionUtil.isNotEmpty(suggestDOS)) {
                suggestService.deleteForLawsuit(lawsuitId);
            }
            lawsuitService.delete(lawsuitId);
            return R.success("删除诉讼事项成功");
        } catch (Exception e) {
            logger.error("删除诉讼事项失败:" + e.getMessage());
            return R.error("删除诉讼事项失败");
        }
    }

    /**
     * 审核诉讼事件
     * 通过或退回
     */
    @RequestMapping("/audit")
    @ResponseBody
    public Result audit(CourtSuggestDO suggestDO) {
        CourtLawsuitDO courtLawsuitDO = new CourtLawsuitDO();
        try {
            courtLawsuitDO.setId(suggestDO.getLawsuitId());
            courtLawsuitDO.setStage(suggestDO.getStage());
            courtLawsuitDO.setUpdateName(getUserName());
            suggestDO.setCheckName(getUserName());
            lawsuitService.update(courtLawsuitDO);
            suggestService.save(suggestDO);
            return R.success("审核成功");
        } catch (Exception e) {
            logger.error("审批失败:" + e.getMessage());
            return R.error("审核失败");
        }
    }

    /**
     * 搜索
     */
    @RequestMapping("/search")
    @ResponseBody
    public Result search(CourtLawsuitQuery courtLawsuitQuery) {
        try {
            if(courtLawsuitQuery.getCreateTime()==null){
               courtLawsuitQuery.setCreateTime(null);
            }
            Integer queryCount = lawsuitService.queryCount(courtLawsuitQuery);
            List<CourtLawsuitDO> courtLawsuitDOS = lawsuitService.queryList(courtLawsuitQuery);
            return R.success(courtLawsuitDOS,queryCount);
        } catch (Exception e) {
            logger.error("查询出错" + e.getMessage());
            return R.error("查询婚姻事项出错");
        }
    }

}
